<?php
/*
	[UCenter Home] (C) 2007-2008 Comsenz Inc.
	$Id: admincp_ask.php
	@author:tong;
	@company:重庆致晓网络；
*/

if(!defined('IN_UCHOME') || !defined('IN_ADMINCP')) {
	exit('Access Denied');
}

//权限
if(!checkperm('managemovie')) {
	cpmessage('no_authority_management_operation');
}
$typekey = array(1=>'app_movie_type',2=>'app_movie_region',3=>'app_movie_plays',4=>'app_moviecredit');
$idname = array(1=>'typeid',2=>'regionid',3=>'playid',4=>'type');
$_GET['rewardtype'] = isset($_GET['rewardtype']) ? intval($_GET['rewardtype']) : 1;
$actives = array($_GET['rewardtype'] => ' class="active"');
$ctable = $typekey[$_GET['rewardtype']];
$idnamekey = $idname[$_GET['rewardtype']];

@include_once(S_ROOT.'./data/data_movie_plays.php');
@include_once(S_ROOT.'./data/data_movie_region.php');
@include_once(S_ROOT.'./data/data_movie_type.php');

if(submitcheck('videosubmit')) {
	//var_dump($_POST);exit();
	$id = intval($_POST['id']);
	$rewardtype = intval($_POST['rewardtype']);
	$table = $typekey[$rewardtype];
	//var_dump($table);exit();
	//判断数据信息；
	$dataarr = array();
	$wherearr = array();
	switch ($rewardtype){
			case '1':
				$wherearr['typeid'] = $id;
				$dataarr['typename'] = $_POST['type'];
				$dataarr['sort'] = intval($_POST['displayorder']);
				break;
			case '2':
				$wherearr['regionid'] = $id;
				$dataarr['regionname'] = $_POST['type'];
				$dataarr['sort'] = intval($_POST['displayorder']);
				break;
			case '3':
				$wherearr['playid'] = $id;
				$dataarr['playername'] = $_POST['type'];
				$dataarr['sort'] = intval($_POST['displayorder']);
				$dataarr['playercontent'] = $_POST['playcode'];
				break;
			case '4':
				$wherearr['type'] = 'movie';
				$dataarr['kanmovie'] = intval($_POST['kanmovie']);
				$dataarr['sendmovie'] = intval($_POST['sendmovie']);
				$dataarr['pingmovie'] = intval($_POST['pingmovie']);
				break;
		}
	//根据判断是插入还是更新；
	if ($id) {
		updatetable($table, $dataarr, $wherearr);	
	}else {
		inserttable($table, $dataarr);
	}
	include_once(S_ROOT.'./source/function_cache.php');
	video_cache();
	if ($rewardtype=='4') {
		$rewardtype = '4&op=add';		
	}
	cpmessage('do_success', "admincp.php?ac=video&rewardtype=$rewardtype");
}elseif (submitcheck('ordersubmit')) {
	//更新排序
	$ctable = $typekey[$_POST['rewardtype']];
	$idnamekey = $idname[$_POST['rewardtype']];
	foreach ($_POST['displayorder'] as $id => $value) {
		//var_dump($ctable.$idnamekey);exit();
		updatetable($ctable, array('sort'=>trim($value)), array($idnamekey=>trim($id)));
	}
	$typefl = $_POST['rewardtype'];
	//更新缓存
	include_once(S_ROOT.'./source/function_cache.php');
	video_cache();
	
	cpmessage('do_success', "admincp.php?ac=video&rewardtype=$typefl");
}

$list = array();

if($_GET['op']=='edit') {
	$thevalue = array();
	//var_dump($ctable.$idnamekey);exit();
	$id = intval($_GET['id']);
	if($id) {
		$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname($ctable)." WHERE $idnamekey='$id'");
		$rule = $_SGLOBAL['db']->fetch_array($query);
		//var_dump($rule);exit();
		if ($_GET['rewardtype']==1) {
			$thevalue['id'] = $rule['typeid'];
			$thevalue['type'] = $rule['typename'];
			$thevalue['sort'] = $rule['sort'];
		}elseif ($_GET['rewardtype']==2){
			$thevalue['id'] = $rule['regionid'];
			$thevalue['type'] = $rule['regionname'];
			$thevalue['sort'] = $rule['sort'];
		}elseif ($_GET['rewardtype']==3){
			$thevalue['id'] = $rule['playid'];
			$thevalue['type'] = $rule['playername'];
			$thevalue['code'] = $rule['playercontent'];
			$thevalue['sort'] = $rule['sort'];
		}
		//var_dump($thevalue);exit();
	}
	if(empty($rule)) {
		cpmessage('there_is_no_designated_video_type', 'admincp.php?ac=video&rewardtype='.$_GET['rewardtype']);
	}
	
} elseif($_GET['op'] == 'delete') {
	if (empty($_GET['rewardtype'])) {
		cpmessage('choose_to_delete_video_typetype', 'admincp.php?ac=video&rewardtype='.$_GET['rewardtype']);
	}
	$cachearr = array('1'=>'movie_type','2'=>'movie_region','3'=>'movie_plays');
	$_GET['id'] = intval($_GET['id']);
	$cachename = $cachearr[$_GET['rewardtype']];
	//var_dump($cachename);
	if(count($_SGLOBAL[$cachename]) < 2) {
		cpmessage('have_one_video_type');
	}
	if (submitcheck('deletesubmit')) {
		//var_dump($cachename);exit();
		$newid = intval($_POST['newfieldid']);
		$rewardtype = $_POST['rewardtype'];
		if(empty($_SGLOBAL[$cachename][$newid])&&$rewardtype!=3) {
			cpmessage('choose_to_delete_video_type');
		}
		
		include_once(S_ROOT.'./source/function_delete.php');
		if($_GET['id'] && deletevideo(array($_GET['id']), $newid, $rewardtype)) {
			//更新缓存
			include_once(S_ROOT.'./source/function_cache.php');
			video_cache();
	
			cpmessage('do_success', 'admincp.php?ac=video&rewardtype='.$rewardtype);
		} else {
			cpmessage('choose_to_delete_video_type', 'admincp.php?ac=video&rewardtype='.$rewardtype);
		}
	}
	$newfield = $_SGLOBAL[$cachename];

	if(isset($newfield[$_GET['id']])) {
		unset($newfield[$_GET['id']]);
	}
	//var_dump($newfield);
	
}elseif ($_GET['rewardtype']=='4'){
	$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname($ctable)." WHERE type='movie'");
	$value = $_SGLOBAL['db']->fetch_array($query);
	//var_dump($value);
} else {
	//var_dump(tname($typekey[$_GET['rewardtype']]));
	$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname($ctable)." ORDER BY `sort` ASC");
	while ($value = $_SGLOBAL['db']->fetch_array($query)) {
		$list[] = fl($_GET['rewardtype'],$value);
	}
	//var_dump($list);
}
//几种分类信息的数据分离；
function fl($a,$value){
	$list = array();
	switch ($a){
			case '1':
				$list[id] = $value[typeid];
				$list[name] = $value[typename];
				$list[sort] = $value[sort];
				break;
			case '2':
				$list[id] = $value[regionid];
				$list[name] = $value[regionname];
				$list[sort] = $value[sort];
				break;
			case '3':
				$list[id] = $value[playid];
				$list[name] = $value[playername];
				$list[playercontent] = $value[playercontent];
				$list[sort] = $value[sort];
				break;
		}
	return $list;
}

?>